package com.petecat.interchan.base.syscustom.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mhuang.common.util.DataUtils;
import com.petecat.interchan.base.syscustom.entity.SysCustom;
import com.petecat.interchan.base.syscustom.entity.SysUserProject;
import com.petecat.interchan.base.syscustom.mapper.ICustomMapper;
import com.petecat.interchan.base.syscustom.service.ICustomService;
import com.petecat.interchan.core.service.impl.BaseServiceImpl;
import com.petecat.interchan.protocol.base.custom.dto.SysCustomAddDTO;
import com.petecat.interchan.protocol.base.custom.dto.SysCustomUpdDTO;
import com.petecat.interchan.protocol.base.custom.vo.SysCustomVO;
import com.petecat.interchan.redis.IdWorker;

@Service
public class CustomServiceImpl extends BaseServiceImpl<SysCustom, String> implements ICustomService{
	
	@Autowired
	private ICustomMapper customMapper;
	
	/**
	 * 通过用户ID查询user_peoject表查询数据
	 */
	@Override
	public List<SysUserProject> findUserProjectByUserId(String userId) {
		 List<SysUserProject> listProject = customMapper.findUserProjectByUserId(userId);
		 if(!listProject.isEmpty()) {
			 return listProject;
		 }
		return null;
	}
	
	/**
	 * 新增User和项目信息
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void saveUserProject(String[] ids, String userId) {
		customMapper.saveUserProject(ids,userId);
	}

	/**
	 * 删除User和项目信息
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void removeUserProjectById(String peojectId, String userId) {
		customMapper.removeUserProjectById(peojectId,userId);
	}
	
	/**
	 * 新增自定义配置信息
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void saveCustomConfig(SysCustomAddDTO sysCustomAddDTO, String userId) {
		SysCustom sysCustom = DataUtils.copyTo(sysCustomAddDTO, SysCustom.class);
		sysCustom.setId(String.valueOf(new IdWorker().nextId()));
		sysCustom.setCreateTime(new Date());
		sysCustom.setCreateUserId(userId);
		sysCustom.setOperaorUserId(userId);
		sysCustom.setOperatorTime(new Date());
		sysCustom.setStatus("1");
		customMapper.save(sysCustom);
	}
	
	/**
	 * 修改自定义配置信息
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void updateCustomConfig(SysCustomUpdDTO sysCustomAddDTO, String userId) {
		SysCustom sysCustom = DataUtils.copyTo(sysCustomAddDTO, SysCustom.class);
		sysCustom.setOperaorUserId(userId);
		sysCustom.setOperatorTime(new Date());
		customMapper.update(sysCustom);
	}
	
	/**
	 * 通过项目ID获取配置
	 */
	@Override
	public SysCustomVO getCustomByProjectId(String projectId, String userId) {
		return  DataUtils.copyTo(customMapper.getCustomByProjectId(projectId,userId), SysCustomVO.class);
	}
	
	
	
}
	